package net.huashitong.user.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.Department;
import net.huashitong.user.entity.Role;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.filter.DepartmentFilter;
import net.huashitong.user.filter.RoleFilter;
import net.huashitong.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/roleManageSuper")
public class RoleManageController extends JsonBaseController {

    @Autowired
    private RoleService roleService;

    @Override
    protected String getView(String s) {
        return "/modules/roleManage/" + s;
    }


    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        String branch = request.getParameter("branch");
        model.addAttribute("leftType", "用户管理");
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("user", bnpUser);
        model.addAttribute("nodeCount", nodeCount);
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);
        if (StringUtils.isNotBlank(branch)) {
            model.addAttribute("branch", branch);
        } else {
            model.addAttribute("branch", "");
        }
        return getView("query");
    }


    //初始化数据
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute RoleFilter filter) {
        List<Role> list = roleService.queryByFilter(filter);
        return coverJqGrid(filter, list);
    }

    /**
     * 创建角色
     */
    @RequestMapping(value = "/createRole", method = RequestMethod.GET)
    public String createDept(@ModelAttribute BnpUserFilter filter) {
        return getView("createRole");
    }
    /**
     * 创建角色
     **/
    @ResponseBody
    @RequestMapping(value = "/createRole", method = RequestMethod.POST)
    public Message createDepartmentInfo(@ModelAttribute RoleFilter filter) {
        roleService.addRole(filter);
        return coverMessage("200", "角色添加成功");
    }

    /**
     * 修改角色
     */
    @RequestMapping(value = "/editRole", method = RequestMethod.GET)
    public String editDept(@RequestParam String sysId, Model model) {
        Role role = roleService.getById(sysId);
        model.addAttribute("role", role);
        return getView("editRole");
    }
    /**
     * 修改角色
     **/
    @ResponseBody
    @RequestMapping(value = "/editRole", method = RequestMethod.POST)
    public Message editDeptInfo(@ModelAttribute Role newRole) throws NoSuchFieldException {
        String sysId = newRole.getSysId();
        if(newRole.getSysId().contains(",")){
            String [] arr = newRole.getSysId().split(",");
            sysId = arr[0];
            newRole.setSysId(sysId);
        }
        Role oldRole = roleService.getById(sysId);
        roleService.updateRole(oldRole,newRole);
        return coverMessage("200", "角色修改成功");
    }
}
